﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace LI4.Data
{
    class Registos
    {
        private Ligacao ligacao;

        public Registos()
        {
            ligacao = new Ligacao();
        }

        public int inserir(Business.Registo registo)
        {
            int success;
            try
            {
                ligacao.Update("INSERT [dbo].[registos] ( [username], [descricao], [data]) VALUES ('" + registo.Username + "','" + registo.Descricao + "','" +registo.Data + "')");
                success = Ligacao.OK;
            }
            catch
            {
                success =  Ligacao.DBERROR;
            }
            return success;
        }

        public ArrayList getRegistos(string username)
        {
            ArrayList array = new ArrayList();
            SqlDataReader reader;

            ligacao.Select("SELECT * FROM Registos where username='" + username + "'");
            reader = ligacao.mySqlDataReader;

            while (reader.Read())
            {
                array.Add(new Business.Registo(reader["username"].ToString(), reader["descricao"].ToString(), reader["data"].ToString()));
            }
            reader.Close();
            return array;
        }

        public ArrayList getLastRegs()
        {
            ArrayList array = new ArrayList();
            SqlDataReader reader;

            ligacao.Select("SELECT TOP 20 * FROM registos ORDER BY data DESC");
            reader = ligacao.mySqlDataReader;

            while (reader.Read())
            {
                array.Add(new Business.Registo(reader["username"].ToString(), reader["descricao"].ToString(), reader["data"].ToString()));
            }
            reader.Close();
            return array;
        }

        public ArrayList getRegistosPC(string pc)
        {
            ArrayList array = new ArrayList();
            SqlDataReader reader;

            ligacao.Select("select username,descricao,data from registos where username like '%" + pc + "%' or descricao like '%" + pc + "%' ORDER BY data DESC");
            reader = ligacao.mySqlDataReader;

            while (reader.Read())
            {
                array.Add(new Business.Registo(reader["username"].ToString(), reader["descricao"].ToString(), reader["data"].ToString()));
            }
            reader.Close();
            return array;
        }
    }
}
